package com.nulldev.util.graphics.renderIt.exts.awt.fonts;

import java.awt.Font;
import java.io.File;

import com.nulldev.util.VariableAPI.MathUtil;
import com.nulldev.util.graphics.renderIt.api.core.RITFont;
import com.nulldev.util.gui.fontAPI.FontManager;

public class AWTFont implements RITFont {

	private final Font f;

	public AWTFont(final Font font) {
		this.f = font;
	}

	@Override
	public RITFont deriveToSize(final float size) {
		return new AWTFont(f.deriveFont(size));
	}

	@Override
	public RITFont deriveToSize(final int size) {
		return new AWTFont(f.deriveFont((float) size));
	}

	@Override
	public RITFont deriveToStyle(final int style) {
		switch (style) {
			case RIT_FONT_VARIANT_NORMAL:
				return new AWTFont(f.deriveFont(Font.PLAIN));
			case RIT_FONT_VARIANT_ITALIC:
				return new AWTFont(f.deriveFont(Font.ITALIC));
			case RIT_FONT_VARIANT_BOLD:
				return new AWTFont(f.deriveFont(Font.BOLD));
			case RIT_FONT_VARIANT_SMALL_CAPS:
			case RIT_FONT_VARIANT_EXTRA_BOLD:
			case RIT_FONT_VARIANT_CUSTOM_WEIGHT:
				/* unsupported */
			default:
				return this;
		}
	}

	@Override
	public String getFontName() {
		return f.getFontName();
	}

	@Override
	public int getFontVariant() {
		switch (f.getStyle()) {
			case Font.ITALIC:
				return RIT_FONT_VARIANT_ITALIC;
			case Font.BOLD:
				return RIT_FONT_VARIANT_BOLD;
			case Font.PLAIN:
			default:
				return RIT_FONT_VARIANT_NORMAL;
		}
	}

	@Override
	public float getFontSize() {
		return f.getSize2D();
	}

	@Override
	public void close() throws Exception {
	}

	@Override
	public Object getFontObject() {
		return this.f;
	}

	public static RITFont create(final String name, final float size) {
		return new AWTFont(FontManager.gets(name).getFont(size));
	}

	public static RITFont create(final String name, final float size, final int style) {
		int awtStyle = Font.PLAIN;
		switch (style) {
			case RIT_FONT_VARIANT_NORMAL:
			case RIT_FONT_VARIANT_CUSTOM_WEIGHT:
			case RIT_FONT_VARIANT_EXTRA_BOLD:
			case RIT_FONT_VARIANT_SMALL_CAPS:
				break;
			case RIT_FONT_VARIANT_BOLD:
				awtStyle = Font.BOLD;
				break;
			case RIT_FONT_VARIANT_ITALIC:
				awtStyle = Font.ITALIC;
				break;
		}
		return new AWTFont(FontManager.gets(name).getFont(size).deriveFont((int) awtStyle));
	}

	public static RITFont create(final File file) {
		throw new UnsupportedOperationException("Unimplemented function: create(File)");
	}

	public static RITFont from(final Font font) {
		return new AWTFont(font);
	}

	@Override
	public String toString() {
		return "AWTFont[hash=" + MathUtil.toHex(super.hashCode()) + ",font=" + this.f + "]";
	}

}
